Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class cls_tarea_

    Implements ICloneable

#Region "Variables ..."
    Private int_idtarea As Integer = 0
    Private dte_fecha As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private dte_hora As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private str_ubicacion As String = ""
    Private int_importancia As enu_importancia = 0
    Private int_orden As Integer = 0
    Private int_idtercero As Integer = 0
    Private str_descripcion As String = ""
    Private str_notas As String = ""
    Private bit_pendiente As Boolean = False
    Private dte_vencimiento As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private dte_vencimientoRE As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private int_tipo As enu_tipo = 0
    Private int_idrecordatorio As Integer = 0
    Private int_idregistro As Integer = 0

    Private obj_registro As cls_registro_ = Nothing

    Private obj_recordatorio As cls_recordatorio_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_tarea_) = Nothing


    Public Enum enu_tipo
        <Description("Visita")> visita = 0
        <Description("Llamada")> llamada = 1
        <Description("Evento")> evento = 2
    End Enum

    Public Enum enu_importancia
        <Description("Baja")> baja = 0
        <Description("Media")> media = 1
        <Description("Alta")> alta = 2
    End Enum

    Public Overrides Function ToString() As String
        Return "(" & EnumDescription(tipo) & ") " & descripcion
    End Function
#End Region

#Region "Propiedades ..."
    Property idtarea() As Integer
        Get
            idtarea = int_idtarea
        End Get
        Set(ByVal value As Integer)
            int_idtarea = value
        End Set
    End Property
    Property fecha() As DateTime
        Get
            fecha = dte_fecha
        End Get
        Set(ByVal value As DateTime)
            dte_fecha = value
        End Set
    End Property
    Property hora() As DateTime
        Get
            hora = dte_hora
        End Get
        Set(ByVal value As DateTime)
            dte_hora = value
        End Set
    End Property
    Property ubicacion() As String
        Get
            ubicacion = str_ubicacion
        End Get
        Set(ByVal value As String)
            str_ubicacion = value
        End Set
    End Property
    Property importancia() As enu_importancia
        Get
            importancia = int_importancia
        End Get
        Set(ByVal value As enu_importancia)
            int_importancia = value
        End Set
    End Property
    Property orden() As Integer
        Get
            orden = int_orden
        End Get
        Set(ByVal value As Integer)
            int_orden = value
        End Set
    End Property
    Property idtercero() As Integer
        Get
            idtercero = int_idtercero
        End Get
        Set(ByVal value As Integer)
            int_idtercero = value
        End Set
    End Property
    Property descripcion() As String
        Get
            descripcion = str_descripcion
        End Get
        Set(ByVal value As String)
            str_descripcion = value
        End Set
    End Property
    Property notas() As String
        Get
            notas = str_notas
        End Get
        Set(ByVal value As String)
            str_notas = value
        End Set
    End Property
    Property pendiente() As Boolean
        Get
            pendiente = bit_pendiente
        End Get
        Set(ByVal value As Boolean)
            bit_pendiente = value
        End Set
    End Property
    Property vencimiento() As DateTime
        Get
            vencimiento = dte_vencimiento
        End Get
        Set(ByVal value As DateTime)
            dte_vencimiento = value
        End Set
    End Property
    Property vencimientoRE() As DateTime
        Get
            vencimientoRE = dte_vencimientoRE
        End Get
        Set(ByVal value As DateTime)
            dte_vencimientoRE = value
        End Set
    End Property
    Property tipo() As enu_tipo
        Get
            tipo = int_tipo
        End Get
        Set(ByVal value As enu_tipo)
            int_tipo = value
        End Set
    End Property
    Property idrecordatorio() As Integer
        Get
            idrecordatorio = int_idrecordatorio
        End Get
        Set(ByVal value As Integer)
            int_idrecordatorio = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property

    Property recordatorio() As cls_recordatorio_
        Get
            If IsNothing(obj_recordatorio) Then
                obj_recordatorio = New cls_recordatorio_
                If Not obj_recordatorio.Buscar(idrecordatorio) Then
                    obj_recordatorio = Nothing
                End If
            End If
            recordatorio = obj_recordatorio
        End Get
        Set(ByVal value As cls_recordatorio_)
            obj_recordatorio = value
        End Set
    End Property
    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property

    Property Items() As Collections.Generic.List(Of cls_tarea_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_tarea_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_tarea_)
        While obj_Reader.Read()
            Dim Elemento As New cls_tarea_
            Elemento.int_idtarea = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtarea")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
                Elemento.dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hora"))) = False Then
                Elemento.dte_hora = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hora")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ubicacion"))) = False Then
                Elemento.str_ubicacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ubicacion")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importancia"))) = False Then
                Elemento.int_importancia = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importancia")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("orden"))) = False Then
                Elemento.int_orden = (obj_Reader.GetValue(obj_Reader.GetOrdinal("orden")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
                Elemento.int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion"))) = False Then
                Elemento.str_descripcion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("notas"))) = False Then
                Elemento.str_notas = (obj_Reader.GetValue(obj_Reader.GetOrdinal("notas")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("pendiente"))) = False Then
                Elemento.bit_pendiente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("pendiente")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento"))) = False Then
                Elemento.dte_vencimiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoRE"))) = False Then
                Elemento.dte_vencimientoRE = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoRE")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
                Elemento.int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecordatorio"))) = False Then
                Elemento.int_idrecordatorio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecordatorio")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idtarea = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtarea")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha"))) = False Then
            dte_fecha = (obj_Reader.GetValue(obj_Reader.GetOrdinal("fecha")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hora"))) = False Then
            dte_hora = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hora")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ubicacion"))) = False Then
            str_ubicacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ubicacion")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importancia"))) = False Then
            int_importancia = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importancia")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("orden"))) = False Then
            int_orden = (obj_Reader.GetValue(obj_Reader.GetOrdinal("orden")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero"))) = False Then
            int_idtercero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtercero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion"))) = False Then
            str_descripcion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("descripcion")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("notas"))) = False Then
            str_notas = (obj_Reader.GetValue(obj_Reader.GetOrdinal("notas")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("pendiente"))) = False Then
            bit_pendiente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("pendiente")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento"))) = False Then
            dte_vencimiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoRE"))) = False Then
            dte_vencimientoRE = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoRE")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo"))) = False Then
            int_tipo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("tipo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecordatorio"))) = False Then
            int_idrecordatorio = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecordatorio")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try

            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------

            'Creacion de recordatorio----------------------------------------------------
            If Not IsNothing(obj_recordatorio) Then
                obj_recordatorio.Nuevo(True)
                idrecordatorio = obj_recordatorio.idrecordatorio
            End If
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tarea__Nuevo"
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@hora", SqlDbType.DateTime).Value = dte_hora
                .Parameters.Add("@ubicacion", SqlDbType.NVarChar).Value = str_ubicacion
                .Parameters.Add("@importancia", SqlDbType.Int).Value = int_importancia
                .Parameters.Add("@orden", SqlDbType.Int).Value = int_orden
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@descripcion", SqlDbType.NVarChar).Value = str_descripcion
                .Parameters.Add("@notas", SqlDbType.NVarChar).Value = str_notas
                .Parameters.Add("@pendiente", SqlDbType.Bit).Value = bit_pendiente
                .Parameters.Add("@vencimiento", SqlDbType.DateTime).Value = dte_vencimiento
                .Parameters.Add("@vencimientoRE", SqlDbType.DateTime).Value = dte_vencimientoRE
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .Parameters.Add("@idrecordatorio", SqlDbType.Int).Value = int_idrecordatorio
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de tarea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try

            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------

            'Modificacion de recordatorio------------------------------------------------
            recordatorio.Modificar()
            '------------------------------------------------------------------------

            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tarea__Modificar"
                .Parameters.Add("@idtarea", SqlDbType.Int).Value = int_idtarea
                .Parameters.Add("@fecha", SqlDbType.DateTime).Value = dte_fecha
                .Parameters.Add("@hora", SqlDbType.DateTime).Value = dte_hora
                .Parameters.Add("@ubicacion", SqlDbType.NVarChar).Value = str_ubicacion
                .Parameters.Add("@importancia", SqlDbType.Int).Value = int_importancia
                .Parameters.Add("@orden", SqlDbType.Int).Value = int_orden
                .Parameters.Add("@idtercero", SqlDbType.Int).Value = int_idtercero
                .Parameters.Add("@descripcion", SqlDbType.NVarChar).Value = str_descripcion
                .Parameters.Add("@notas", SqlDbType.NVarChar).Value = str_notas
                .Parameters.Add("@pendiente", SqlDbType.Bit).Value = bit_pendiente
                .Parameters.Add("@vencimiento", SqlDbType.DateTime).Value = dte_vencimiento
                .Parameters.Add("@vencimientoRE", SqlDbType.DateTime).Value = dte_vencimientoRE
                .Parameters.Add("@tipo", SqlDbType.Int).Value = int_tipo
                .Parameters.Add("@idrecordatorio", SqlDbType.Int).Value = int_idrecordatorio
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de tarea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idtarea As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tarea__Eliminar"
                .Parameters.Add("@idtarea", SqlDbType.Int).Value = idtarea
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de tarea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idtarea As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tarea__Buscar"
                .Parameters.Add("@idtarea", SqlDbType.Int).Value = idtarea
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de tarea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tarea__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de tarea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "tarea__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de tarea_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_tarea_Clon As New cls_tarea_
        obj_tarea_Clon.int_idtarea = Me.int_idtarea
        obj_tarea_Clon.dte_fecha = Me.dte_fecha
        obj_tarea_Clon.dte_hora = Me.dte_hora
        obj_tarea_Clon.str_ubicacion = Me.str_ubicacion
        obj_tarea_Clon.int_importancia = Me.int_importancia
        obj_tarea_Clon.int_orden = Me.int_orden
        obj_tarea_Clon.int_idtercero = Me.int_idtercero
        obj_tarea_Clon.str_descripcion = Me.str_descripcion
        obj_tarea_Clon.str_notas = Me.str_notas
        obj_tarea_Clon.bit_pendiente = Me.bit_pendiente
        obj_tarea_Clon.dte_vencimiento = Me.dte_vencimiento
        obj_tarea_Clon.dte_vencimientoRE = Me.dte_vencimientoRE
        obj_tarea_Clon.int_tipo = Me.int_tipo
        obj_tarea_Clon.int_idrecordatorio = Me.int_idrecordatorio
        obj_tarea_Clon.int_idregistro = Me.int_idregistro
        obj_tarea_Clon.col_Items = Me.col_Items
        Return obj_tarea_Clon
    End Function

End Class
